<?php
namespace Home\Controller;
class MyarticleController extends HomeController
{


    public function count()
    {
        $user = M('article');
        // 查找已发布文章的条数
        $article_count = $user->where(array('status'=>'1','user_id'=>$_SESSION['user']['id']))->count();
        // 查找未通过文章的条数
        $killed_article_count = $user->where(array('user_id'=>$_SESSION['user']['id'],'status'=>'2'))->count();
        // 查找审核中的文章的条数
        $verify_article_count = $user->where(array('status'=>'0','user_id'=>$_SESSION['user']['id']))->count();
        // 查找草稿数
        $draft = M('draft_article');
        $draft_article_count=$draft->where(array('user_id'=>$_SESSION['user']['id']))->count();
        $this->assign('article_count',$article_count);
        $this->assign('killed_article_count',$killed_article_count);
        $this->assign('verify_article_count',$verify_article_count);
        $this->assign('draft_article_count',$draft_article_count);

    }
    public function article()
    {
        $_GET['p'] = empty($_GET['p'])?'1':$_GET['p'];
        $m=M('article');
        $count = $m->where(array('user_id'=>$_SESSION['user']['id'],'status'=>'1'))->count();

        $p=getpage($m,$count,5);
        $list=$m->where(array('status'=>'0'))->order('id DESC')->page($_GET['p'].',10')->select();
        $list=$m->where(array('user_id'=>$_SESSION['user']['id'],'status'=>'1'))->order('id desc')->field('id,title,create_time,content,time,cover')->page($_GET['p'].',5')->select();
        $this->list=$list;
        $this->assign('article',$list);// 赋值数据集
        $this->page=$p->show();
        // $this->assign('msg',$data);


        $this->count();
        // $this->assign('article',$article);
        $this->display('Myarticle/article');
    }

    public function killed_article()
    {
        $_GET['p'] = empty($_GET['p'])?'1':$_GET['p'];

         $m = M('article');
        // $killed_article=$user->where(array('user_id'=>$_SESSION['user']['id'],'status'=>'2'))->order('id desc')->field('id,title,create_time,content,time,cover')->select();


        $count = $m->where(array('user_id'=>$_SESSION['user']['id'],'status'=>'2'))->count();

        $p=getpage($m,$count,10);
        $list=$m->where(array('user_id'=>$_SESSION['user']['id'],'status'=>'2'))->order('id desc')->page($_GET['p'].',10')->field('id,title,create_time,content,time,cover')->select();


        $this->list=$list;
        $this->assign('killed_article',$list);// 赋值数据集
        $this->page=$p->show();
        $this->count();
        // $this->assign('killed_article',$killed_article);
        $this->display('Myarticle/killed_article');
    }

    public function verify_article()
    {
        $_GET['p'] = empty($_GET['p'])?'1':$_GET['p'];
         $m = M('article');
        // $verify_article=$user->where(array('user_id'=>$_SESSION['user']['id'],'status'=>'0'))->order('id desc')->field('id,title,create_time,content,time,cover')->select();
         
        $count = $m->where(array('user_id'=>$_SESSION['user']['id'],'status'=>'0'))->count();

        $p=getpage($m,$count,5);
        $list=$m->where(array('user_id'=>$_SESSION['user']['id'],'status'=>'0'))->order('id desc')->field('id,title,create_time,content,time,cover')->page($_GET['p'].',5')->select();

        $this->list=$list;
        $this->assign('verify_article',$list);// 赋值数据集
        $this->page=$p->show();
        $this->count();
        // $this->assign('verify_article',$verify_article);
        $this->display('Myarticle/verify_article');
    }
    public function draft_article()
    {

       $_GET['p'] = empty($_GET['p'])?'1':$_GET['p'];
        $m = M('draft_article');

       // $draft=$user->where(array('user_id'=>$_SESSION['user']['id']))->order('id desc')->select();
       $count = $m->where(array('user_id'=>$_SESSION['user']['id']))->count();
       $p=getpage($m,$count,5);

       $list=$m->where(array('user_id'=>$_SESSION['user']['id']))->order('id desc')->page($_GET['p'].',5')->select();
       $this->list=$list;
       $this->assign('draft',$list);// 赋值数据集
       $this->page=$p->show();
       $this->count();
        // $this->assign('draft',$draft);
        $this->display('Myarticle/draft_article');
    }

    public function save_draft()
    {

        if ($_POST['content'] == '' || $_POST['title'] == '') {
            $this->redirect('Contribute/index');
            exit;
        }
        $_POST['content'] =htmlentities($_POST['content']);
        $_POST['title'] = htmlentities($_POST['title']);
        $_POST['user_id'] = $_SESSION['user']['id'];
        $_POST['time'] = time();

        // V($_POST);exit;
        M('draft_article')->create();
        if (M('draft_article')->add()) {
            $this->success('您已成功发布!','draft_article');
            exit;
        } else {
            $this->error('发布失败!');
        }
    }

    public function del_draft()
    {
        $id=I('get.aid/d',0);
        $info = M('draft_article');
        $info ->where(array('id'=>$id))->delete();
        if ($info) {
        $this->redirect('draft_article');
        }
    }


}